home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
EnigmA Amiga Run 1997 February
/
EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso
/
enigma
/
onlypc
/
java
/
samples.bin
/
AnimationPanel.java
next >
Wrap
Text File
|
1996-03-03
|
4KB
|
168 lines
/*
This class is a basic extension of the Panel class. It can be used by
either an applet or application. To use it, add an instance to any
subclass of Container.
example:
add(new AnimationPanel());
//use validate if the add is done outside of
//an applets init or a Containers constructor.
validate();
You can add controls to AnimationPanel with Cafe Studio, but not menus. (Menus
can only be added to subclasses of Frame.)
*/
import java.awt.*;
public class AnimationPanel extends Panel
{
PolygonAnimation theAnimation;
public AnimationPanel(Color color, int delay)
{
super();
setBackground(color);
//{{INIT_CONTROLS
setLayout(new BorderLayout());
buttonPanel=new Panel();
add("South",buttonPanel);
buttonPanel.reshape(8,214,212,48);
aPanel=new Panel();
add("Center",aPanel);
aPanel.reshape(4,4,216,210);
startButton=new Button("Start");
buttonPanel.add(startButton);
startButton.reshape(34,18,60,24);
stopButton=new Button("Stop");
buttonPanel.add(stopButton);
stopButton.reshape(120,18,60,24);
//}}
aPanel.add(theAnimation = new PolygonAnimation(delay));
}
public void ResetAnimation()
{
theAnimation.stop();
theAnimation.position = 0;
theAnimation.repaint();
}
public boolean handleEvent(Event event) {
if (event.id == Event.ACTION_EVENT && event.target == stopButton) {
StopAnimation();
return true;
}
else
if (event.id == Event.ACTION_EVENT && event.target == startButton) {
StartAnimation();
return true;
}
return super.handleEvent(event);
}
//{{DECLARE_CONTROLS
Panel buttonPanel;
Panel aPanel;
Button startButton;
Button stopButton;
//}}
public void StartAnimation() {
theAnimation.resume();
}
public void StopAnimation() {
theAnimation.stop();
}
}
/*
This class is a simple extention of Canvas that runs as it's own
thread. It runs a simple animation.
*/
class PolygonAnimation extends Canvas implements Runnable
{
Thread myThread;
Polygon thePolygon;
int polyX[] = new int[9], //The polygons X coordinates
polyY[] = new int[9], //The polygons Y coordinates
position, //The point on the polygon the line is drawn to
thedelay;
public PolygonAnimation(int delay)
{
resize(130, 130);
SetupPolygon();
thePolygon = new Polygon(polyX, polyY, 9);
position = 0;
if(delay < 0 || delay > 300) delay = 100;
else thedelay = delay;
myThread = new Thread(this);
myThread.start();
}
public void resume() { myThread.resume(); }
public void run()
{
while (true)
{
repaint();
try{ Thread.sleep(thedelay); } catch(InterruptedException e) { this.stop(); }
advance();
}
}
public void stop() { myThread.suspend(); }
public void destroy() { this.stop(); }
//Draw the polygon and a line from the center to the
//specified position.
public void paint(Graphics g)
{
g.drawPolygon(thePolygon);
g.drawLine(65, 65, polyX[position], polyY[position]);
}
private void advance()
{
if(position < 6) position++;
else position = 0;
}
//Set up the two arrays with the X and Y coordinate values
private void SetupPolygon()
{
polyX[0] = 37;
polyX[1] = 97;
polyX[2] = 129;
polyX[3] = 129;
polyX[4] = 97;
polyX[5] = 37;
polyX[6] = 1;
polyX[7] = 1;
polyX[8] = 37;
polyY[0] = 1;
polyY[1] = 1;
polyY[2] = 37;
polyY[3] = 97;
polyY[4] = 129;
polyY[5] = 129;
polyY[6] = 97;
polyY[7] = 37;
polyY[8] = 1;
}
}